package prototype.controller.message;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import prototype.constant.EMessageConst;
import prototype.constant.UrlConst;
import prototype.controller.AbstractController;

import javax.persistence.EntityManager;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Comparator;

/**
 * Просмотр списка сообщений
 */
@WebServlet("/" + UrlConst.URL_MESSAGE_LIST)
public class MessageListController extends AbstractController {

	public static final String URL = UrlConst.URL_MESSAGE_LIST;

	@Override
	protected JsonObject execute(EntityManager entityManager, HttpSession session, JsonElement requestData) throws Exception {
		final JsonObject jsonObject = new JsonObject();
		final JsonArray jsonArray = new JsonArray();
		final EMessageConst array[] = EMessageConst.values();
		Arrays.sort(array, new Comparator<EMessageConst>() {
			@Override
			public int compare(EMessageConst o1, EMessageConst o2) {
				return o1.name().compareTo(o2.name());
			}
		});
		for (final EMessageConst item : array) {
			jsonArray.add(item.toJsonObject());
		}

		jsonObject.add("messageList", jsonArray);
		return jsonObject;
	}
}
